<?php

	class SignController extends  BaseController{
		
		protected  $_className ="SignRecords";

		public function getRecordsList($filter=null){ 
			
			return parent::getEntityList($filter,null,null,null,null,0);
		}

		public function getPersonalList($sign_date = null){
			$filter = new SqlFilter();
			if($sign_date!=null){
				$filter->addExpress(" DATEDIFF(m,sign_date,'".$sign_date."')=0 ");
			}
			// 通过user的getloginuserid方法获取用户id并进行过滤查询
			$user_id = Users::GetLoginUserID();
			if(empty($user_id)){
				$user_id=-1;
			}
			$filter->addFilter("user_id",EQUALS,$user_id);
			return parent::getEntityList($filter,null,null,null,null,0);
		}

		public function getSingleRecord($id){
			return  parent::getEntityByid($id, true);
		}

		public function addSignRecord($post_json,$transaction = null){
			$material = new Material(AccessTokenManager::getAccessToken());
			$mediaIds = $post_json['photos'];

			$this->_bean = $this->getPostEntity($post_json);
			$this->_bean->set("user_id",Users::GetLoginUserID());
			$this->_bean->set("shop_id",Users::GetLoginShopID());
			
			for($i=0;$i<count($mediaIds);$i++){
				$stream = $material->getPermanentMaterial($mediaIds[$i]);
				$fileName = Utils::guid().".jpg";
				$floderName  = date('Y-m-d', time());
				$filePath = Utils::getPath(ROOT_PATH.DIRECTORY_SEPARATOR.UPLOAD_FILE_ROOT_DIR.DIRECTORY_SEPARATOR.$floderName).DIRECTORY_SEPARATOR.$fileName;		
				@file_put_contents($filePath, $stream);

				$photo= AbstractBean::LoadBean("SignPhotos");
				$photo->set("upload_name",$fileName);
				$photo->set("save_name",$fileName);
				$photo->set("sub_path",$floderName);
				$photo->set("url","/backend/".UPLOAD_FILE_ROOT_DIR."/".$floderName."/".$fileName);
				$photo->set("upload_time",date('Y-m-d H:i:s', time()));
				$this->_bean->addChildRecord($photo);
			}

			$this->_bean->save();		
		}

		/*public function saveRecord($transaction = null){
			// $this->_bean->set("shop_type",SHOP);
			parent::save($post_json,$transaction);
		}*/
		
		public function getInterdayRecords($user_id,$sign_date){
			$filter = new SqlFilter("user_id",EQUALS,$user_id);
			$filter->addExpress(" DATEDIFF(d,sign_date,'".$sign_date."')=0 ");
			return parent::getBeanList("SignRecords",$filter,null,0,true);
		}


		public function getShopDistance($lat,$lng){
			$distance = PageUtils::getDistance($lat,$lng);
			$arr = array('distance' => $distance);
			$arr['shop'] = $_SESSION["shop"];
			return $arr;
		}


		public function getViewRecords($filter){
			return parent::getEntityList($filter,null,"ViewSignRecords");
		}

		

	}
?>